

// object转query字符
export const makeQuery = (queryObject) => {
  const query = Object.entries(queryObject).reduce((result, entry) => {
    result.push(entry.join('='));
    return result;
  }, []).join('&');
  return `?${query}`;
};

// 关闭所有页面跳转某页面
export const reLaunch = (page, params = {}) => {
  let query = makeQuery(params);
  uni.reLaunch({
    url: page + query,
  });
};

// 页面路由跳转
export const jumpPage = (page, params = {}) => {
  let url = page
  if(JSON.stringify(params) != '{}') {
    let query = makeQuery(params);
    url = page + query
  }
  uni.navigateTo({ url });
};
// 页面路由跳转
export const hideJumpPage = (page, params = {}) => {
  let url = page
  if(JSON.stringify(params) != '{}') {
    let query = makeQuery(params);
    url = page + query
  }
  uni.redirectTo({ url });
};

// tab跳转
export const jumpTab = (page) => {
  uni.switchTab({
    url: page,
  });
};

// 页面直接跳转
export const jump = (uri) => {
  const tapPage = [
    '/pages/index/index',
    // '/pages/goods/lists',
    // '/pages/goods/class',
    // '/pages/cart/cart',
    // '/pages/user/index',
  ];
  if (tapPage.indexOf(uri) === -1) {
    uni.navigateTo({
      url: uri,
    });
  } else {
    uni.switchTab({
      url: uri,
    });
  }
};

export const toDecimal = (x) => {
  var f = parseFloat(x);
  if (isNaN(f)) {
    return;
  }
  f = Math.round(x * 100) / 100;
  return f;
};


// 获取当前页面uri
export const curUri = () => {
  let routes = getCurrentPages();
  let curRoute = routes[routes.length - 1].route;
  let curParam = routes[routes.length - 1].options;
  let param = '';
  for (let key in curParam) {
    param += '&' + key + '=' + curParam[key];
  }

  if (param === '') {
    return '/' + curRoute;
  }

  return '/' + curRoute + '?' + param;
};

export const back = () => {
  uni.navigateBack();
};
